//
// Created by 张亦乐 on 2018/9/18.
//

#ifndef PEONY_MSGCENTER_HPP
#define PEONY_MSGCENTER_HPP

#include <atomic>
#include <memory>

#include "lotus/ConcurrentQueue.hpp"
#include "net/Worker.hpp"
#include "net/TcpConnector.hpp"

#include "proto/MsgPacket.pb.h"

using PacketPtr = std::shared_ptr<ProtoMsg::Packet>;
struct MsgTask
{
    TcpConnectorPtr    conn;
    PacketPtr          packet;
	// uint64_t		ts;
};
class MsgCenter
{
private:
    ConcurrentQueue<MsgTask>    _msgs;
    Worker                      _worker;
    std::atomic_bool            _quit;
private:
    void loop();
public:
    MsgCenter();
    ~MsgCenter()= default;
	template<class T>
	void PushProtoMsg(T& msg)
	{
		PacketPtr packetPtr(new ProtoMsg::Packet());
		packetPtr->set_name(T::descriptor()->full_name());
		packetPtr->set_content(msg.SerializeAsString());
		packetPtr->set_seq(0);
		packetPtr->set_code(0);
		_msgs.push(MsgTask{TcpConnectorPtr(),packetPtr});
	}
    bool Parse(Buffer&,TcpConnectorPtr&);
    void Start();
};
#endif //PEONY_MSGCENTER_HPP
